<?php
/**
 * clase simplificada
 * -se agrego getNext and getPreview
 * @version 2.6
 * @author tuadmin
 */
class paginator
{
	private $resultsPerPage = 9;
	private $currentPage = 0;
	private $totalResults = 0;
	private $navLinkTotal = 9;
	private $numPages = 0;
	private $currentPages;
	private $pageNext = 1;
	private $pagePreview = 1;
	/**
	 * @param int $num_total_pages
	 */
	static public function newInstance($num_total_pages)
	{
		return new self($num_total_pages);
	}
	public function __construct($num_total_pages)
	{
		$this->totalResults = $num_total_pages;
		$this->currentPages = new ArrayIterator();
	}
	public function setTotalLinks($n)
	{
		$this->navLinkTotal = $n;
	}
	public function currentPage($page)
	{
		$this->currentPage = $page < 0 ? 0:floor($page);
	}
	public function resultsPerPage($int = 9)
	{
		$this->resultsPerPage = $int;
	}
	/**
	 * @return void
	 * @tutorial $this->pagiante() is a process esencial
	 */
	 public function paginate()
	 {
	 	$this->numPages = ceil($this->totalResults / $this->resultsPerPage);
		$this->numPages = $this->numPages;
		$this->currentPage = ($this->currentPage >= $this->numPages)?$this->numPages:$this->currentPage ;
		$this->currentPage = ($this->currentPage < 1)?1:$this->currentPage ;
		$div_after = ceil($this->navLinkTotal/2);
		$div_before = $this->navLinkTotal - $div_after;
		$desde = $this->getBeforeCurrentPage($div_before);
		$hasta = $this->getAfterCurrentPage($div_after);
		$this->navLinkTotal ;
		if(($desde +  $this->navLinkTotal) > $this->numPages)
		{
			$desde = ($this->numPages - $this->navLinkTotal) +1 ;
			$hasta = $desde + $this->navLinkTotal;
			$desde = $desde < 1? 1:$desde;
		}
		else
		{

			$hasta = $desde + $this->navLinkTotal;
		}
		for($i = $desde ;$i < $hasta && $i <= $this->numPages ; $i++)
		{
			$this->currentPages->append($i);
		}
		/*process get and next page*/
		$this->pagePreview = $this->getBeforeCurrentPage(1);
		$this->pageNext = $this->getAfterCurrentPage(1);
	 }
	public function totalPages()
	{
		return $this->numPages;
	}
	public function currentPages()
	{
		return $this->currentPages;
	}
	/**
	 * @return ArrayIterator
	 * */
	public function ArrayPages()
	{
		return $this->currentPages;
	}
	public function getPages()
	{
		return $this->currentPages;
	}
	public function limitSql()
	{
		return ("LIMIT ".($this->resultsPerPage * ($this->currentPage-1)).",".$this->resultsPerPage);
	}
	public function __toString()
	{
		return 'LIMIT '.($this->resultsPerPage * ($this->currentPage -1)).",".$this->resultsPerPage;
	}
	public function getLast()
	{
		$calcular = $this->getFirst() + $this->resultsPerPage;
		if($calcular > $this->totalResults)
		{
			$calcular = $this->totalResults - $this->getFirst();
			$calcular = $calcular + $this->getFirst();
		}
		return $calcular;
	}
	public function getFirst()
	{
		$resultado = ($this->resultsPerPage * ($this->currentPage-1));
		return $resultado;

	}
	public function getCurrentPage()
	{
		return $this->currentPage;
	}
	public function getBeforeCurrentPage($n_positions)
	{
		$before = $this->currentPage - $n_positions;
		if($before > 1)
		{
			return $before;
		}
		else
		{
			return 1;
		}
	}
	public function getAfterCurrentPage($n_positions)
	{
		$after = $this->currentPage + $n_positions;
		if($after > $this->numPages)
		{
			return $this->numPages;
		}
		else
		{
			return $after;
		}
	}
	public function getPreview()
	{
		return $this->pagePreview;
	}
	public function getNext()
	{
		return $this->pageNext;
	}
}

/*
$paginateSql = new paginator(10);
		$paginateSql->paginator->resultsPerPage(1);
		$paginateSql->currentPage(URL::GET('page'));
		$paginateSql->paginate();
		foreach ($paginateSql->ArrayPages AS $page)
		{
			echo "  - $page -  ";
		}


$archivo = file("data4.txt");
$lineas = count($archivo);

$actual = $_GET['actual'];
$ejemplo = new paginator($lineas);
//resultados por pagina
$ejemplo->resultsPerPage(5);
//pagina actual la que se esta viendo
$ejemplo->currentPage($acual);
//procesar todo para generar
$ejemplo->paginate();

//rescatar resultado para mysql
//esta parte lo modificas para usarlo en tu script osea modificas el 'metodo'
$ejemplo->limitSql();
/**/
